#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：PythonData 
@File    ：case5_xpath.py
@Author  ：朱志文
@Date    ：2021/12/29 10:53 
'''

'''淘宝商品也爬虫'''
import requests,re
from lxml import etree

key=str(input("选择搜索商品信息"))
# for page in range(11):
resp = requests.get(
    url=f'https://s.taobao.com/search?q={key}&bcoffset=1&ntoffset=1&p4ppushleft=2%2C48&s=44',
    headers={
            'authority': 's.taobao.com',
            'cache-control': 'max-age=0',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'referer': 'https://www.taobao.com/',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': 't=b24fed4fa0826a860525630b2a99ab51; cna=wlssGgsNoiMCAT2ZlnKhqbtb; xlly_s=1; lgc=%5Cu68A7%5Cu6850slsher; tracknick=%5Cu68A7%5Cu6850slsher; enc=hUtf%2Fgc8B2EJLZbt%2FBLUUGgYGa7sNmm87koQM2%2BXEvmMggQQ83bj8MMtH7nnXfpa7hLjNgWZe%2BLHbdu%2F9XYCag%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; _m_h5_tk=892d730b13233909b4d5a79c2b240882_1640764191512; _m_h5_tk_enc=8605756389a75ee4b42950f9060e28b2; _tb_token_=e0e5e335e447e; cookie2=279844a77c372f60947b33f8fcd3060e; alitrackid=www.taobao.com; lastalitrackid=www.taobao.com; _samesite_flag_=true; sgcookie=E1008RC%2FQ5Y2sdCnWsz8MxmTFLLehfGPhifKKXxvi9zhD5bFqYAIlZrlC7u2gt0QO9SGjnFUGQb%2Fl69n9h%2FyWXw%2BZkjIRrTRHHKAu0C95JvYYCRmmbNVNxqfPUSb0DUjwK1w; unb=3168299097; uc3=id2=UNGQV%2BKLgR18mQ%3D%3D&nk2=rXlpmyv6hezr6g%3D%3D&vt3=F8dCvUs%2BCjpdcyQE%2F1A%3D&lg2=URm48syIIVrSKA%3D%3D; csg=78840510; cancelledSubSites=empty; cookie17=UNGQV%2BKLgR18mQ%3D%3D; dnk=%5Cu68A7%5Cu6850slsher; skt=f402a080a6ded080; existShop=MTY0MDc1OTU0Mw%3D%3D; uc4=nk4=0%40r4ibE1dFNBrt3Osw9q3PLeFVACHA&id4=0%40Ugbo%2BfkiaI6uZ7GlsOBg67ZWwTCP; _cc_=WqG3DMC9EA%3D%3D; _l_g_=Ug%3D%3D; sg=r7d; _nk_=%5Cu68A7%5Cu6850slsher; cookie1=U%2BS%2BExuwHviMqg0EP9AZloVVMiwhXqnTPLZ5HrYZsIA%3D; mt=ci=8_1; _uab_collina=164076005494735890356527; uc1=cookie21=V32FPkk%2FgihF%2FS5nr3O5&cookie16=W5iHLLyFPlMGbLDwA%2BdvAGZqLg%3D%3D&existShop=false&cookie14=UoewAMU%2BRysbOw%3D%3D&cookie15=Vq8l%2BKCLz3%2F65A%3D%3D&pas=0; x5sec=7b227365617263686170703b32223a223537323131323331633161343337333063303364393532633366623638363538434a795273493447454a4f7439644f55752f58647a77456144444d784e6a67794f546b774f5463374d6a436e68594b652f502f2f2f2f3842227d; JSESSIONID=8A80CDD87F8E669A4C9573AF7E766EE7; l=eBNSY36ngeE-jAUkBOfwourza77O-IRAguPzaNbMiOCPOD5H55LhW6dpwoLMCnGVh6FyR35T8LL0BeYBqCmWfdW22j-laQkmn; isg=BMfHKI68apLtPe4ZRXPAbhacVnuRzJuuAFbcwpm0p9Z9COfKoZxN_grCqshW4HMm; tfstk=cN7lBiAIVg-5RlxcfzT5iFVt5oEOZToBIT6OuNt2B-JViO_VianqbouQXLw_UD1..',
    }
    )
    # goods=re.findall("\"raw_title\":\".*?\"",resp.text)
    # prices=re.findall("\"view_price\":\".*?\"",resp.text)
    # for good,price in zip(goods,prices):
    #     print('{:<12}'.format(price.split("\"")[-2]),end='')
    #     print('{:<32}'.format(good.split("\"")[-2]),end='')
    #     print()

print(resp.text)
tree=etree.HTML(resp.text)
title=tree.xpath('//script[@view_price]')
print(title)

